package com.lyy.common;

import org.apache.commons.codec.binary.Base64;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class ImageBase64 {
    public static void main(String[] args) {
        String s = "name.jpf";
        int i = s.indexOf(".");
        System.out.println(i);
        System.out.println(s.substring(0,i));
        System.out.println(s.substring(i+1));
    }

    //将文件转换成String
    public static String multipartFileToString(MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            int i = originalFilename.indexOf(".");
            File file1 = File.createTempFile(originalFilename.substring(0,i),originalFilename.substring(i));
            file.transferTo(file1);
            FileInputStream fis = new FileInputStream(file1);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            String s = Base64.encodeBase64String(data);
            try {
                file1.deleteOnExit();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     将文件转换成String
     */
    public static String fileToString(String pathStr) {
        File file = new File(pathStr);
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            return Base64.encodeBase64String(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     将文件转换成String
     */
    public static String fileToString(File file) {
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            return Base64.encodeBase64String(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * //将String转换成文件
     * @param base64file 是上面的方法转换后的字符串
     * @param filePath 是文件保存路径
     * @param fileName 是文件名
     */
    public static void stringToFile(String base64file, String filePath, String fileName) {
        byte[] bytes = Base64.decodeBase64(base64file);
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            // 判断文件目录是否存在
            if (!dir.exists() && dir.isDirectory()) {
                dir.mkdirs();
            }
            file = new File(filePath + "\\" + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
